import java.io.*;
import java.util.*;  


/*
 * This Class Handles the input and output of the Class Score Application
 */
public class ClassScoreIO {
	
	// The HighScore
	private double HighScore = 0;
	// The LowScore
	private double LowScore = 0;

	/*
	 * ReadFile Takes in a FileName
	 * This function takes the contents of a file and puts it into a String
	 */
	public String ReadFile(String FileName)
	{
		try
		{
			
	      BufferedReader BR =  new BufferedReader(new FileReader(FileName));
		    
			String FileString = "";
			String strLine = "";

			while ((strLine = BR.readLine()) != null)   {
			      // Print the content on the console
				FileString = FileString + strLine ;
			    }
			
			return FileString;

		}
		catch(Exception E)
		{
		System.out.println(E.getMessage());	
		return "";
		}
		
	}
	
	/*
	 * ReadGradeFromFile
	 * This Function Pulls Grades from a file and puts them into an ArrayList
	 */
	public ArrayList<Integer> ReadGradeFromFile(String GradeFileName)
	{
		try
		{
			ArrayList<Integer> Grades = new ArrayList<Integer>();
			String GradesString = ReadFile(GradeFileName);
			
			String NewGrade = "";
			char CurChr; 
			for (int i = 0; i < GradesString.length(); i++)
			{
				CurChr = GradesString.charAt(i);
				if (Character.isDigit(CurChr))
				{
					NewGrade += CurChr;				
				}
				else
					if (NewGrade != "")
						{
							Grades.add(Integer.parseInt(NewGrade));
							NewGrade = "";
						}
			}
			if (NewGrade != "")
			{
				Grades.add(Integer.parseInt(NewGrade));
			}

			return Grades;
		}
		catch(Exception E)
		{
		System.out.println(E.getMessage());	
		return null;
		}
	}

	/*
	 *  WriteToFile This File takes in a File name and outputs the Results
	 */
	public void WriteToFile(String GradeFile)
	{
		WriteToFile(ReadGradeFromFile(GradeFile));
	}
	
	/*
	 * This function Takes in a ArrayList of Grades and outputs the results
	 */
	public void WriteToFile(ArrayList<Integer> Grades)
	{
		try
		{
			ClassScoreMath CM = new ClassScoreMath();
			
			HighScore = CM.CalcHighScore(Grades);
			LowScore = CM.CalcLowScore(Grades);

			//Collections.sort(Grades);
			
		FileWriter MyFile = new FileWriter("ClassScoreOutput.txt");

		MyFile.write("Avg Score = " + CM.CalcAvg(Grades) + "\r\n");
		MyFile.write("Above Avg = " + HighScore + "\r\n");
		MyFile.write("Below Avg = " + LowScore + "\r\n" + "\r\n");

		MyFile.write("  Score  |  Grade \r\n");
		MyFile.write("============================ \r\n");
		for (int i = 0; i < Grades.size(); i++)
		{
			MyFile.write("   " + Grades.get(i).toString() + "    |   " + ScoreToGrade(Grades.get(i)) + "\r\n");
		}
		
		MyFile.close();
		}
		catch(Exception E)
		{
		System.out.println(E.getMessage());	
		}
	}
	
	/*
	 * ScoreToGrade this function takes in a Score and Based on the Class Vars HighScore
	 *  	and LowScore returns a Grade Level
	 */
	public String ScoreToGrade(int Score)
	{
		if (Score > HighScore)
		{
			return "Outstanding";
		}
		if (Score < LowScore)
		{
			return "Unsatisfactory";
		}
		return "Satisfactory";
		
	}
}
